using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody rb;
    public InputAction move;

    private Flip flip;
    private AnimationController anim;

    [Header("Speed Settings")]
    [SerializeField] private float initialSpeed;
    [SerializeField] private float currentspeed;
    [SerializeField] private float animationDistance;
    [SerializeField] private float animationLength;

    private void Awake()
    {
        flip = GetComponent<Flip>();
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<AnimationController>();
    }

    private void Start()
    {
        currentspeed = initialSpeed;
    }

    private void FixedUpdate()
    {
        OnMove();
    }

    #region Actions
    private void OnEnable()
    {
        move.Enable();
    }

    private void OnDisable()
    {
        move.Disable();
    }
    #endregion

    #region Movement
    private void OnMove()
    {
        Vector2 input = move.ReadValue<Vector2>();
        Vector3 direction = new Vector3(input.x, 0f, input.y);

        if (direction.magnitude > 0.1f)
        {
            flip.SetDirection(direction);
            rb.linearVelocity = direction.normalized * currentspeed;
            anim.SetAnim("walk", currentspeed, animationDistance, animationLength);
        }
        else
        {
            rb.linearVelocity = Vector3.zero;
            anim.SetAnim("idle", currentspeed, animationDistance, animationLength);
        }
    }
    #endregion

    #region Requests
    public void SetSpeed(float newSpeed)
    {
        currentspeed = newSpeed;
    }

    public float GetSpeed()
    {
        return currentspeed;
    }
    #endregion
}